<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Lists extends CI_Model {

	var $_table_name = 'list';
	var $_table_user = 'user';

	public function __construct() {
    	$this->load->database();
  	}

  	public function get_list($limit, $offset){

  		$this->db->select('l.id, l.title, l.intro, l.publish_time, l.user_id, u.nick, u.user_pic');
  		$this->db->from($this->_table_name . ' AS l');
  		$this->db->join($this->_table_user . ' AS u', 'l.user_id = u.user_id','left');

  		$this->db->limit($limit,$offset);
  		$query = $this->db->get();

  		return $query->result_array();
  	}

    public function get_count(){

        $this->db->select('*');
        $this->db->from($this->_table_name);

        $query = $this->db->get();

        return $query->num_rows();
    }

    public function get_my_list($user_id){

        $this->db->select('l.id, l.title, l.intro, l.publish_time, l.user_id, u.nick, u.user_pic');
        $this->db->from($this->_table_name . ' AS l');
        $this->db->join($this->_table_user . ' AS u', 'l.user_id = u.user_id','left');

        $this->db->where('l.user_id', $user_id);
        $query = $this->db->get();

        return $query->result_array();
    }

  	public function publish($insertData){

  		$this->db->insert($this->_table_user, $insertData);
  		return $this->db->insert_id();
  	}

    public function get_one($id){
        $this->db->select('l.id, l.title, l.intro, l.publish_time, l.user_id, u.nick, u.user_pic');
        $this->db->from($this->_table_name . ' AS l');
        $this->db->join($this->_table_user . ' AS u', 'l.user_id = u.user_id','left');

        $this->db->where('l.id', $id);
        $query = $this->db->get();

        return $query->row_array();
    }

}

